﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Converters;
using System.Windows.Media.Composition;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GadgetBase
{

    public partial class SystemButtonPanel : UserControl
    {
        private SizeMode sizeMode = SizeMode.Large;
        private Size defaultSize;
        private Grid grid;
        private DoubleAnimation activateAnimation;
        private DoubleAnimation disableAnimation;

        private DragPanel dp;

        public event GadgetWindowEventHandler CloseButtonClick;
        public event GadgetWindowEventHandler SettingsButtonClick;
        public event GadgetWindowEventHandler DragPanelDown;
        public event GadgetWindowEventHandler DragPanelUp;
        public event MouseButtonEventHandler DragPanelMouseLeftButtonDown
        {
            add
            {
                dp.MouseLeftButtonDown += value;
            }
            remove
            {
                dp.MouseLeftButtonDown -= value;
            }
        }

        public SystemButtonPanel()
        {
            base.Content = new Grid();

            base.SnapsToDevicePixels = true;

            OuterGlowBitmapEffect effect = new OuterGlowBitmapEffect();
            effect.GlowColor = Colors.White;
            effect.GlowSize = 1;
            effect.Opacity = 0;


            CloseButton cb = new CloseButton();
            cb.Name = "cb";
            cb.BitmapEffect = effect;
            cb.VerticalAlignment = VerticalAlignment.Top;
            cb.MouseEnter += new MouseEventHandler(Button_MouseEnter);
            cb.MouseLeave += new MouseEventHandler(Button_MouseLeave);
            cb.MouseDown += new MouseButtonEventHandler(CloseButton_MouseDown);

            SettingsButton sb = new SettingsButton();
            sb.Name = "cb";
            sb.BitmapEffect = effect.Clone();
            sb.VerticalAlignment = VerticalAlignment.Top;
            sb.MouseEnter += new MouseEventHandler(Button_MouseEnter);
            sb.MouseLeave += new MouseEventHandler(Button_MouseLeave);
            sb.MouseDown += new MouseButtonEventHandler(SettingsButton_MouseDown);

            dp = new DragPanel();
            dp.Name = "dp";
            dp.BitmapEffect = effect.Clone();
            dp.VerticalAlignment = VerticalAlignment.Top;
            dp.MouseEnter += new MouseEventHandler(Button_MouseEnter);
            dp.MouseLeave += new MouseEventHandler(Button_MouseLeave);
            dp.MouseDown += new MouseButtonEventHandler(DragPanel_MouseDown);
            dp.MouseUp += new MouseButtonEventHandler(DragPanel_MouseUp);

            this.grid = this.Content as Grid;
            this.grid.Children.Add(cb);
            this.grid.Children.Add(sb);
            this.grid.Children.Add(dp);

            OnSizeModeChanged();

            activateAnimation = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(0.1), FillBehavior.HoldEnd);
            disableAnimation = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(0.1), FillBehavior.HoldEnd);
        }

        public SizeMode SizeMode
        {
            get
            {
                return this.sizeMode;
            }
            set
            {
                this.sizeMode = value;
                OnSizeModeChanged();
            }
        }

        public Size DefaultSize
        {
            get
            {
                return defaultSize;
            }
        }

        public void ResetSize()
        {
            this.Height = this.DefaultSize.Height;
            this.Width = this.DefaultSize.Width;
        }

        private void Button_MouseEnter(object sender, MouseEventArgs e)
        {
            UserControl uc = sender as UserControl;
            (uc.BitmapEffect as OuterGlowBitmapEffect).BeginAnimation(OuterGlowBitmapEffect.OpacityProperty, activateAnimation);
        }
        private void Button_MouseLeave(object sender, MouseEventArgs e)
        {
            UserControl uc = sender as UserControl;
            (uc.BitmapEffect as OuterGlowBitmapEffect).BeginAnimation(OuterGlowBitmapEffect.OpacityProperty, disableAnimation);
        }

        private void CloseButton_MouseDown(object sender, MouseEventArgs e)
        {
            OnCloseButtonClick();
        }

        private void SettingsButton_MouseDown(object sender, MouseEventArgs e)
        {
            OnSettingsButtonClick();
        }

        private void DragPanel_MouseDown(object sender, MouseEventArgs e)
        {
            OnDragPanelDown();
        }

        private void DragPanel_MouseUp(object sender, MouseEventArgs e)
        {
            OnDragPanelUp();
        }


        protected virtual void OnCloseButtonClick()
        {
            if (CloseButtonClick != null)
            {
                CloseButtonClick();
            }
        }

        protected virtual void OnSettingsButtonClick()
        {
            if (SettingsButtonClick != null)
            {
                SettingsButtonClick();
            }
        }

        protected virtual void OnDragPanelDown()
        {
            if (DragPanelDown != null)
            {
                DragPanelDown();
            }
        }

        protected virtual void OnDragPanelUp()
        {
            if (DragPanelUp != null)
            {
                DragPanelUp();
            }
        }

        protected virtual void OnSizeModeChanged()
        {
            double width = 0, height = 0;
            foreach (SystemButton button in this.grid.Children)
            {
                button.SizeMode = this.sizeMode;
                button.Margin = new Thickness(0, height, 0, 0);
                if (button.Width > width)
                {
                    width = button.Width;
                }
                height += button.Height;
            }
            this.defaultSize = new Size(width, height);
            ResetSize();
        }
    }
}
